package com.dahua.messageconsumer.service;

import org.apache.activemq.artemis.jms.client.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.*;

@Service
public class Producer {
    public final Topic TOPIC_EQDATA = new ActiveMQTopic("TPC_EQDATA");
    public final Topic TOPIC_CONTROL56 = new ActiveMQTopic("TPC_CONTROL56");
    public final Topic TOPIC_UPLOAD = new ActiveMQTopic("TPC_UPLOAD");
    public final String PROP_EQID = "eqId";

    public final Topic TOPIC_EQDATA_CONSUMER = new ActiveMQTopic("TPC_EQDATA_CONSUMER");
    public final Topic TOPIC_CONTROL56_CONSUMER = new ActiveMQTopic("TPC_CONTROL56_CONSUMER");
    public final Topic TOPIC_UPLOAD_CONSUMER = new ActiveMQTopic("TPC_UPLOAD_CONSUMER");

    public final String QUEUE_EQDATA_CONSUMER = "data_consumer";
    public final String QUEUE_CONTROL56_CONSUMER = "control56_consumer";
    public final String QUEUE_UPLOAD_CONSUMER = "upload_consumer";

    public final String QUEUE_EQDATA_PORTAL = "data_portal";
    public final String QUEUE_CONTROL56_PORTAL = "control56_portal";
    public final String QUEUE_UPLOAD_PORTAL = "upload_portal";
    public final String QUEUE_EQUIPMENTFAULT_PORTAL = "equipmentfault_portal";

    public final String QUEUE_EQDATA_IOT = "data_iot";
    public final String QUEUE_CONTROL56_IOT = "control56_iot";

    public final String QUEUE_ENERGY = "energy";

    public final String QUEUE_SQL = "data_sql";
    public final String FIRST_SQL = "first_sql";
    public final String EQUIPMENTFAULT_SQL = "eqf_sql";

    @Autowired
    private JmsTemplate jmsTemplate;

    public void sendMessage(int equipmentid, String dataType, byte[] data) {

        // 区分： @PostMapping("/data") @PostMapping("/control56") @PostMapping("/upload")
        // 区分不同队列。一般而言 control56  和 upload 信息量很少。主要是 data
        // upload 只需要一个
        String queue = String.format("%s_%d",dataType, equipmentid);

        jmsTemplate.send(queue, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createObjectMessage(data);
            }
        });

    }
    /*
     目的：
     （1）深圳地铁用于发送发送数据给安全研究院
     （2）用于采集部分生产数据来模拟
     */
    public void sendMessage2(int equipmentid, String dataType, byte[] data) {
        String queue = String.format("%s_%d_2",dataType, equipmentid); // queue名称 _2

        jmsTemplate.send(queue, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createObjectMessage(data);
            }
        });

    }
    public void sendEnergy(String data) {
        jmsTemplate.send(QUEUE_ENERGY, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(data);
            }
        });

    }

    // 主题 发布订阅模式解耦合 不使用队列那种点对点的
    public void sendMessageToTopic(Topic topic, int eqId, byte[] data) {
        jmsTemplate.send(topic, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                BytesMessage bytesMessage = session.createBytesMessage();
                bytesMessage.setIntProperty(PROP_EQID, eqId);
                bytesMessage.writeBytes(data);
                return bytesMessage;
            }
        });
    }

    public void sendSqlMessage(int equipmentid, String dataType, String data) {

        String queue = String.format("%s_%d",dataType, equipmentid);

        jmsTemplate.send(queue, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(data);
            }
        });

    }
}
